﻿/* 3. We are given a matrix of strings of size N x M. 
 * Sequences in the matrix we define as sets of several neighbor elements located on the same line, column or diagonal. 
 * Write a program that finds the longest sequence of equal strings in the matrix. */

using System;

public class MaxSequenceOfEqualNeighbours
{
    public static int n;
    public static int m;
    public static string[,] matrix;

    public static int maxLength;
    public static string element;
    public static int severalElements;

    public static int counterR;
    public static int counterC;
    public static int counterD;
    public static int counterBD;

    public static int maxSymbols;

    public static void Main()
    {
        DataInput();
        PrintMatrix();
        CheckAllPositions();
        PrintResult();
    }

    public static void DataInput()
    {
        int symbols = 0;

        Console.BackgroundColor = ConsoleColor.Black;
        Console.ForegroundColor = ConsoleColor.White;

        do
        {
            Console.Clear();
            Console.Write("\nN = ");
            if (int.TryParse(Console.ReadLine(), out n) && (0 < n))
                break;
        } while (true);

        do
        {
            Console.Clear();
            Console.Write("\nN = {0}\nK = ", n);
            if (int.TryParse(Console.ReadLine(), out m) && (0 < m))
                break;
        } while (true);

        Console.WriteLine();

        matrix = new string[n, m];

        for (int row = 0; row < n; row++)
        {
            for (int col = 0; col < m; col++)
            {
                Console.Write("matrix[{0}, {1}] = ", row, col);
                matrix[row, col] = Console.ReadLine();

                symbols = matrix[row, col].ToString().Length;
                if (maxSymbols < symbols)
                    maxSymbols = symbols;
            }

            Console.WriteLine();
        }
    }

    public static void PrintMatrix()
    {
        for (int i = 0; i < n; i++)
        {
            Console.Write("{0}{1}", new string(' ', (1 + maxSymbols - matrix[i, 0].ToString().Length)), matrix[i, 0]);
            for (int j = 1; j < m; j++)
                Console.Write("{0}{1}", new string(' ', (1 + maxSymbols - matrix[i, j].ToString().Length)), matrix[i, j]);

            Console.WriteLine();
        }

        Console.WriteLine();
    }

    public static void CheckAllPositions()
    {
        int length;

        for (int row = 0; row < n; row++)
            for (int col = 0; col < m; col++)
            {
                length = Math.Max(Math.Max(CheckDiagonal(row, col), CheckBackDiagonal(row, col)),
                                    Math.Max(CheckRow(row, col), CheckColumn(row, col)));

                if (maxLength < length)
                {
                    maxLength = length;
                    element = matrix[row, col];
                    severalElements = 0;

                    if (length == 1)
                        severalElements = 1;
                    else if (length > 1)
                    {
                        if (counterD == length)
                            severalElements++;

                        if (counterBD == length)
                            severalElements++;

                        if (counterR == length)
                            severalElements++;

                        if (counterC == length)
                            severalElements++;
                    }
                }
                else if (maxLength == length)
                {
                    if (length == 1)
                        severalElements++;
                    else
                    {
                        if (counterD == length)
                            severalElements++;

                        if (counterBD == length)
                            severalElements++;

                        if (counterR == length)
                            severalElements++;

                        if (counterC == length)
                            severalElements++;
                    }
                }
            }

        severalElements /= maxLength;
    }

    public static int CheckDiagonal(int row, int col)
    {
        counterD = 1;

        int x = row - 1;
        int y = col - 1;

        while ((0 <= x) && (0 <= y))
        {
            if (matrix[x--, y--] == matrix[row, col])
                counterD++;
            else
                break;
        }

        x = row + 1;
        y = col + 1;

        while ((x < n) && (y < m))
        {
            if (matrix[x++, y++] == matrix[row, col])
                counterD++;
            else
                break;
        }

        return counterD;
    }

    public static int CheckBackDiagonal(int row, int col)
    {
        counterBD = 1;

        int x = row - 1;
        int y = col + 1;

        while ((0 <= x) && (y < m))
        {
            if (matrix[x--, y++] == matrix[row, col])
                counterBD++;
            else
                break;
        }

        x = row + 1;
        y = col - 1;

        while ((x < n) && (0 <= y))
        {
            if (matrix[x++, y--] == matrix[row, col])
                counterBD++;
            else
                break;
        }

        return counterBD;
    }

    public static int CheckRow(int row, int col)
    {
        counterR = 1;

        int y = col - 1;

        while (0 <= y)
        {
            if (matrix[row, y--] == matrix[row, col])
                counterR++;
            else
                break;
        }

        y = col + 1;

        while (y < m)
        {
            if (matrix[row, y++] == matrix[row, col])
                counterR++;
            else
                break;
        }

        return counterR;
    }

    public static int CheckColumn(int row, int col)
    {
        counterC = 1;

        int x = row - 1;

        while (0 <= x)
        {
            if (matrix[x--, col] == matrix[row, col])
                counterC++;
            else
                break;
        }

        x = row + 1;

        while (x < n)
        {
            if (matrix[x++, col] == matrix[row, col])
                counterC++;
            else
                break;
        }

        return counterC;
    }

    public static void PrintResult()
    {
        if (severalElements > 1)
            Console.Write("One of all {0} sequences with maximal length ({1}) of equal neighbours is:\n", severalElements, maxLength);
        else
            Console.Write("The unique sequence with maximal length ({0}) of equal neighbours is:\n", maxLength);

        Console.BackgroundColor = ConsoleColor.Red;
        Console.ForegroundColor = ConsoleColor.White;

        Console.Write(element);
        for (int i = 1; i < maxLength; i++)
            Console.Write(" {0}", element);

        Console.BackgroundColor = ConsoleColor.Black;
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("\n");
    }
}